package com.czwj.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czwj.entities.DataGridData;
import com.czwj.entities.Log;
import com.czwj.entities.PageBean;
import com.czwj.entities.UserInfo;
import com.czwj.service.LogService;
import com.czwj.util.TextUtil;

@Controller
public class LogAction{
	@Resource
	LogService logService;
	
	@RequestMapping(value = "/log",method = RequestMethod.GET)
	@ResponseBody
	public DataGridData<Log> logGrid(@RequestParam(value = "page",required = false) String page,
			@RequestParam(value = "rows",required = false) String rows,
			@RequestParam(value = "fromDate",required = false) String fromDate,
			@RequestParam(value = "toDate",required = false) String toDate) throws Exception{
		DataGridData<Log> result = new DataGridData();
		Map<String,Object> map = new HashMap<String,Object>();
		PageBean pageBean = null;
		if(TextUtil.isNotEmpty(page) && TextUtil.isNotEmpty(rows)){
			pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		}
		if(pageBean != null){
			map.put("start",pageBean.getStart());
			map.put("size",pageBean.getPageSize());
		}
		if(TextUtil.isNotEmpty(fromDate)){
			map.put("fromDate",fromDate);
		}
		if(TextUtil.isNotEmpty(toDate)){
			map.put("toDate",toDate);
		}
		Subject currentUser = SecurityUtils.getSubject();
		UserInfo userInfo = (UserInfo)currentUser.getSession().getAttribute("userInfo");
		if(userInfo.getId()==1){
			
		}else{
			map.put("companyid",userInfo.getCompanyid());
		}
		List<Log> list = logService.find(map);
		result.setRows(list);
		result.setTotal(logService.getCount(map));
		return result;
	}
}
